package com.ocom.company.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ocom.common.constant.UserConstants;
import com.ocom.common.entity.company.EntityCompanyInfo;
import com.ocom.common.entity.master.EntityCompanyList;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.common.utils.StringUtils;
import com.ocom.company.dao.IDaoCompanyInfo;
import com.ocom.company.dao.IDaoCompanyListInfo;
import com.ocom.common.dto.CompanyInfoDto;
import com.ocom.company.service.CompanyInfoService;

import com.ocom.company.vo.GetCompanyInfoVo;
import com.ocom.mysql.aspect.DataSourceAspect;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;


@Service
public class CompanyInfoServiceImpl extends ServiceImpl<IDaoCompanyInfo, EntityCompanyInfo> implements CompanyInfoService {

    //注入持久层
    @Autowired
    private IDaoCompanyInfo daoCompanyInfo;

    //注入CompanyListInfo持久层
    @Autowired
    private IDaoCompanyListInfo daoCompanyListInfo;

    /**获取单位信息*/
    @DataSourceAspect(type= DataSourceEnum.PART_DB,companyId = "comId")
    @Override
    public EntityCompanyInfo GetCompanyInfo(Long comId, GetCompanyInfoVo getCompanyInfoVo) {

        QueryWrapper<EntityCompanyInfo> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotNull(comId)){
            wrapper.eq("com_id",comId);
        }
        if (StringUtils.isNotNull(getCompanyInfoVo.getAgentId())){
            wrapper.eq("agent_id",getCompanyInfoVo.getAgentId());
        }
        if (StringUtils.isNotEmpty(getCompanyInfoVo.getComName())){
            wrapper.eq("com_name",getCompanyInfoVo.getComName());
        }
        if (StringUtils.isNotNull(getCompanyInfoVo.getIsAgent())){
            wrapper.eq("is_agent",getCompanyInfoVo.getIsAgent());
        }
        if (StringUtils.isNotEmpty(getCompanyInfoVo.getProvince())){
            wrapper.eq("province",getCompanyInfoVo.getProvince());
        }
        if (StringUtils.isNotEmpty(getCompanyInfoVo.getCity())){
            wrapper.eq("city",getCompanyInfoVo.getCity());
        }
        return daoCompanyInfo.selectOne(wrapper);
    }


    /**
     * 添加分库单位信息
     * */
    @DataSourceAspect(type= DataSourceEnum.PART_DB,companyId = "companyId")
    @Transactional
    @Override
    public void AddCompanyInfo(CompanyInfoDto companyInfo,Long companyId) {
        //先执行分库，成功则进入下一步，否则返回错误信息（操作失败：原因（2009））
        EntityCompanyInfo info = new EntityCompanyInfo();
        QueryWrapper<EntityCompanyInfo> companyInfoQueryWrapper = Wrappers.query();
        if (StringUtils.isNotEmpty(companyInfo.getComName())){
            info.setComName(companyInfo.getComName());
        }
        if (StringUtils.isNotNull(companyInfo.getIsAgent())){
            info.setIsAgent(companyInfo.getIsAgent());
        }
        if (StringUtils.isNotNull(companyInfo.getAgentId())){
            info.setAgentId(companyInfo.getAgentId());
        }
        if (StringUtils.isNotNull(companyInfo.getComType())){
            info.setComType(companyInfo.getComType());
        }
        if (StringUtils.isNotNull(companyInfo.getComStatus())){
            info.setComStatus(companyInfo.getComStatus());

        }
        if (StringUtils.isNotEmpty(companyInfo.getProvince())){
            info.setProvince(companyInfo.getProvince());
        }
        if (StringUtils.isNotEmpty(companyInfo.getCity())){
            info.setCity(companyInfo.getCity());
        }
        if (StringUtils.isNotEmpty(companyInfo.getDistrict())){
            info.setDistrict(companyInfo.getDistrict());
        }
        if (StringUtils.isNotEmpty(companyInfo.getContPerson())){
            info.setContPerson(companyInfo.getContPerson());

        }
        if (StringUtils.isNotEmpty(companyInfo.getContPhone())){
            info.setContPhone(companyInfo.getContPhone());
        }
        if (StringUtils.isNotEmpty(companyInfo.getContEmail())){
            info.setContEmail(companyInfo.getContEmail());
        }
        if (StringUtils.isNotEmpty(companyInfo.getContAddress())){
            info.setContAddress(companyInfo.getContAddress());
        }
        if (StringUtils.isNotEmpty(companyInfo.getRemark())){
            info.setRemark(companyInfo.getRemark());
        }
        if (StringUtils.isNotNull(companyInfo.getMjStatus())){
            info.setMjStatus(companyInfo.getMjStatus());
        }
        if (StringUtils.isNotNull(companyInfo.getXfStatus())){
            info.setXfStatus(companyInfo.getXfStatus());
        }
        if (StringUtils.isNotNull(companyInfo.getEndTime())){
            info.setEndTime(companyInfo.getEndTime());
        }
        info.setPayCenter(companyInfo.getPayCenter());
        info.setCreateUserid(companyInfo.getCreateUserid());
        daoCompanyInfo.insert(info);
        companyInfo.setComId(info.getComId());
    }

    /**
     * 更新分库单位信息
     * */
    @DataSourceAspect(type= DataSourceEnum.PART_DB,companyId = "comId")
    @Transactional
    @Override
    public void UpdateCompanyInfo(CompanyInfoDto companyInfo,Long comId) {
        //先执行分库，成功则进入下一步，否则返回错误信息（操作失败：原因（2009））
        EntityCompanyInfo info = new EntityCompanyInfo();
        QueryWrapper<EntityCompanyInfo> companyInfoQueryWrapper = Wrappers.query();
        if (StringUtils.isNotNull(companyInfo.getComId())) {
            companyInfoQueryWrapper.eq("com_id", companyInfo.getComId());
        }
        info.setComName(companyInfo.getComName());
        info.setIsAgent(companyInfo.getIsAgent());
        info.setAgentId(companyInfo.getAgentId());
        info.setComType(companyInfo.getComType());
        info.setComStatus(companyInfo.getComStatus());
        info.setProvince(companyInfo.getProvince());
        info.setCity(companyInfo.getCity());
        info.setDistrict(companyInfo.getDistrict());
        info.setContPerson(companyInfo.getContPerson());
        info.setContPhone(companyInfo.getContPhone());
        info.setContEmail(companyInfo.getContEmail());
        info.setContAddress(companyInfo.getContAddress());
        info.setRemark(companyInfo.getRemark());
        info.setMjStatus(companyInfo.getMjStatus());
        info.setXfStatus(companyInfo.getXfStatus());
        info.setEndTime(companyInfo.getEndTime());
        info.setCreateUserid(companyInfo.getCreateUserid());
        info.setColor(companyInfo.getColor());
        info.setLogoUrl(companyInfo.getLogoUrl());
        info.setCommNums(companyInfo.getCommNums());
        info.setCommType(companyInfo.getCommType());
        info.setPayCenter(companyInfo.getPayCenter());
        info.setUpdateTime(new Date());
        daoCompanyInfo.update(info,companyInfoQueryWrapper);
    }


    //校验单位名称是否唯一
    @Override
    @DataSourceAspect(type= DataSourceEnum.MAIN_DB,isMaster = MasterSlaveEnum.SLAVE)
    public String checkComNameUnique(String comName) {
        QueryWrapper<EntityCompanyList> query = Wrappers.query();
        query.eq("com_name",comName).last("limit 1");
        //校验
        EntityCompanyList entityCompanyList = daoCompanyListInfo.selectOne(query);
        //存在
        if (StringUtils.isNotNull(entityCompanyList) && comName.equals(entityCompanyList.getComName()))   {
            return UserConstants.NOT_UNIQUE;
        }
        //不存在
        return UserConstants.UNIQUE;
    }
    /**
     * 校验单位名称更改条件
     *
     * @param comName 单位名称
     * @return 结果
     */
    @Override
    @DataSourceAspect(type= DataSourceEnum.MAIN_DB,isMaster = MasterSlaveEnum.SLAVE)
    public String checkComNameComId(String comName, Long comId) {
        QueryWrapper<EntityCompanyList> query = Wrappers.query();

        query.eq("com_name",comName).last("limit 1");
        //校验
        EntityCompanyList entityCompanyList = daoCompanyListInfo.selectOne(query);

        //存在
        if (StringUtils.isNotNull(entityCompanyList) && comName.equals(entityCompanyList.getComName()) && !comId.equals(entityCompanyList.getComId()))   {
            return UserConstants.NOT_UNIQUE;
        }
        //不存在
        return UserConstants.UNIQUE;
    }

    @DataSourceAspect(type = DataSourceEnum.PART_DB)
    @Override
    public EntityCompanyInfo getCompanyInfoById(Long companyId) {
        return this.getById(companyId);
    }

    @DataSourceAspect(type = DataSourceEnum.PART_DB)
    @Override
    public void modifyColor(Long companyId, String colorNo) {
        UpdateWrapper<EntityCompanyInfo> updateWrapper=new UpdateWrapper<>();
        updateWrapper.eq("com_id",companyId);
        updateWrapper.set("color",colorNo);
        this.update(updateWrapper);
    }

    @DataSourceAspect(type = DataSourceEnum.PART_DB)
    @Override
    public void modifyName(Long companyId, String companyName) {
        UpdateWrapper<EntityCompanyInfo> updateWrapper=new UpdateWrapper<>();
        updateWrapper.eq("com_id",companyId);
        updateWrapper.set("com_name",companyName);
        this.update(updateWrapper);
    }


    @DataSourceAspect(type = DataSourceEnum.PART_DB)
    @Override
    public void modifyPid(Long companyId, Long pId) {
        UpdateWrapper<EntityCompanyInfo> updateWrapper=new UpdateWrapper<>();
        updateWrapper.eq("com_id",companyId);
        updateWrapper.set("create_userid",pId);
        this.update(updateWrapper);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB)
    public List<EntityCompanyInfo> getCompanyList(Long companyId) {
        QueryWrapper<EntityCompanyInfo> query = Wrappers.query();
        query.select("com_id","com_name");
        return this.list(query);
    }


}
